class Position:

    def __init__(self,*args):

        if len(args)>2 or not args :
            raise TypeError('Position takes a tuple of (x,y) or x,y directly')
        x,y = (args if len(args)-1!=0 else args[0])
        self.x,self.y=(x,y)
    
    def __getitem__(self,num):

        return (self.x,self.y)[num]

    def __cmp__(self,other):
        selfxy,otherxy=tuple(self),tuple(other)

        return cmp(selfxy,otherxy)

    def __repr__(self):

        return 'Pos %s' % repr(tuple(self))

    def __hash__(self):

        return hash(tuple(self))

    def toRect(self,size):

        
        lefttop=self.x-size[0]/2,self.x-size[1]/2

        rightbottom=self.x+size[0]/2,self.y+size[1]/2

        return Rect(lefttop,rightbottom)


class Rect:

    def __init__(self,lefttop,rightbottom):

        self.lefttop,self.rightbottom=Position(*lefttop),Position(*rightbottom)

        self.size=(self.rightbottom.x-self.lefttop.x,self.rightbottom.y-self.lefttop.y)

    def __iter__(self):

        return iter((tuple(self.lefttop),tuple(self.rightbottom)))

    def __cmp__(self,other):

        if tuple(self)==tuple(other):
            return 0
        else :
            return NotImplemented

    def getMidPos(self):

        return Position(
                (self.lefttop.x+self.rightbottom.x)/2,

                (self.lefttop.y+self.rightbottom.y)/2
                )

    def __repr__(self):

        return 'Rect %s' % repr(tuple(self))

    def getCollisions(self,size):

        pos=self.getMidPos()

        if (pos.x % size[0]==size[0]/2) and (pos.y % size[1] == size[1]/2) :
            return (pos,)

        x1,y1=(pos.x-size[0]/2)/size[0]*size[0]+size[0]/2,(pos.y-size[0]/2)/size[1]*size[1]+size[1]/2

        x2,y2=x1+size[0],y1+size[1]

        from itertools import product

        return tuple( [Position(x,y) for x,y in product((x1,x2),(y1,y2))] )
